package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.PunchTimeBean;
import com.attendance.www.hr.dao.mapper.PunchTimeMapper;
import com.attendance.www.hr.entitys.PunchTime;
import com.attendance.www.hr.entitys.PunchTimeExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IPunchTimeService;
import com.attendance.www.hr.utils.TimeUtils;

@Service
public class PunchTimeServiceImpl implements IPunchTimeService {
	@Resource
	private PunchTimeMapper PunchTimeDao;
	
	/**
	 *新增时打卡名称不可重复
	 */
	@Override
	public boolean getSavePunchTimeNameValidate(PunchTimeBean punchTimeBean) {
		PunchTimeExample punchTimeExample = new PunchTimeExample();
	    PunchTimeExample.Criteria punchTimeCriter = punchTimeExample.createCriteria();
	    punchTimeCriter.andPunchTimeNameEqualTo(punchTimeBean.getPunchTimeName());
	    punchTimeCriter.andIsDeleteEqualTo(0);
	    if(punchTimeBean.getPunchTimeId()!=null){
	    	punchTimeCriter.andPunchTimeIdNotEqualTo(punchTimeBean.getPunchTimeId());
	    }
	    
	    List<PunchTime> punchTimeList = PunchTimeDao.selectByExample(punchTimeExample);
	    if(punchTimeList.size()>0){
	    	throw new ResultFaultException("17_00001");
	    }
		return true;
	}

	/**
	 *修改时打卡名称不可重复
	 */
	@Override
	public boolean getUpdatePunchTimeNameValidate(PunchTimeBean punchTimeBean) {
		PunchTime punchTime = PunchTimeDao.selectByPrimaryKey(punchTimeBean.getPunchTimeId());
		if(punchTime==null){
			throw new ResultFaultException("0_00012");
		}
		
		PunchTimeExample punchTimeExample = new PunchTimeExample();
		PunchTimeExample.Criteria punchTimeCriter = punchTimeExample.createCriteria();
	    punchTimeCriter.andPunchTimeNameEqualTo(punchTimeBean.getPunchTimeName());
	    if(punchTimeBean.getPunchTimeId()!=null){
	    	punchTimeCriter.andPunchTimeIdNotEqualTo(punchTimeBean.getPunchTimeId());
	    }
	    List<PunchTime> punchTimeList = PunchTimeDao.selectByExample(punchTimeExample);
	    if(punchTimeList.size()>0){
	    	throw new ResultFaultException("17_00001");
	    }
	    
		return true;
	}


	/**
	 *删除时验证是否存在
	 */
	@Override
	public boolean getDeletePunchTimeNameValidate(String id) {
		PunchTime punchTime = PunchTimeDao.selectByPrimaryKey(Integer.parseInt(id));
		if(punchTime==null){
			throw new ResultFaultException("0_00012");
		}
		return true;
	}
	
	/**
	 * 添加一条考勤设置数据
	 */
	@Override
	public boolean savePunchTime(PunchTimeBean punchTimeBean) {
		String restFromTime=punchTimeBean.getRestFromTime();
		String restToTime=punchTimeBean.getRestToTime();
		if (StringUtils.isEmpty(restFromTime)|| StringUtils.isEmpty(restToTime)) {
			restFromTime="12:00";
			restToTime="12:00";
		}
		PunchTime punchTime=new PunchTime();
		punchTime.setPunchTimeName(punchTimeBean.getPunchTimeName());
		punchTime.setStartFromTime(punchTimeBean.getStartFromTime());
		punchTime.setStartToTime(punchTimeBean.getStartToTime());
		punchTime.setStartInterval(punchTimeBean.getStartInterval());
		punchTime.setEndFromTime(punchTimeBean.getEndFromTime());
		punchTime.setEndToTime(punchTimeBean.getEndToTime());
		punchTime.setEndInterval(punchTimeBean.getEndInterval());
		punchTime.setIsDefault(punchTimeBean.getIsDefault());
		punchTime.setRestFromTime(punchTimeBean.getRestFromTime());
		punchTime.setRestToTime(punchTimeBean.getRestToTime());
		Integer hours=TimeUtils.differMinutes(punchTimeBean.getStartFromTime(), punchTimeBean.getEndFromTime())
				-TimeUtils.differMinutes(restFromTime, restToTime);
		punchTime.setHours(hours);
		int record = PunchTimeDao.insertSelective(punchTime);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取考勤设置列表数据
	 */
	@Override
	public List<PunchTimeBean> getPunchTimeList() {
		PunchTimeExample example=new PunchTimeExample();
		PunchTimeExample.Criteria criteria = example.createCriteria();
		criteria.andIsDeleteEqualTo(0);
		List<PunchTime> punchTimeList=PunchTimeDao.selectByExample(example);
		List<PunchTimeBean> PunchTimeBeanList=new ArrayList<PunchTimeBean>();
		if(punchTimeList!=null){
			for(int i=0;i<punchTimeList.size();i++){
				PunchTimeBean punchTimeBean=new PunchTimeBean();
				punchTimeBean.setPunchTimeId(punchTimeList.get(i).getPunchTimeId());
				punchTimeBean.setPunchTimeName(punchTimeList.get(i).getPunchTimeName());
				punchTimeBean.setEndFromTime(punchTimeList.get(i).getEndFromTime());
				punchTimeBean.setEndInterval(punchTimeList.get(i).getEndInterval());
				punchTimeBean.setEndToTime(punchTimeList.get(i).getEndToTime());
				punchTimeBean.setStartFromTime(punchTimeList.get(i).getStartFromTime());
				punchTimeBean.setStartInterval(punchTimeList.get(i).getStartInterval());
				punchTimeBean.setStartToTime(punchTimeList.get(i).getStartToTime());
				punchTimeBean.setIsDefault(punchTimeList.get(i).getIsDefault());
				punchTimeBean.setUpdateTime(punchTimeList.get(i).getUpdateTime());
				punchTimeBean.setRestFromTime(punchTimeList.get(i).getRestFromTime());
				punchTimeBean.setRestToTime(punchTimeList.get(i).getRestToTime());
				PunchTimeBeanList.add(punchTimeBean);
			}
		}
		return PunchTimeBeanList;
	}

	/**
	 * 修改一条考勤设置数据
	 */
	@Override
	public boolean updatePunchTime(PunchTimeBean punchTimeBean) {
		String restFromTime=punchTimeBean.getRestFromTime();
		String restToTime=punchTimeBean.getRestToTime();
		if (StringUtils.isEmpty(restFromTime)|| StringUtils.isEmpty(restToTime)) {
			restFromTime="12:00";
			restToTime="12:00";
		}
		PunchTime punchTime=new PunchTime();
		punchTime.setPunchTimeId(punchTimeBean.getPunchTimeId());
		punchTime.setPunchTimeName(punchTimeBean.getPunchTimeName());
		punchTime.setStartFromTime(punchTimeBean.getStartFromTime());
		punchTime.setStartToTime(punchTimeBean.getStartToTime());
		punchTime.setStartInterval(punchTimeBean.getStartInterval());
		punchTime.setEndFromTime(punchTimeBean.getEndFromTime());
		punchTime.setEndToTime(punchTimeBean.getEndToTime());
		punchTime.setEndInterval(punchTimeBean.getEndInterval());
		punchTime.setIsDefault(punchTimeBean.getIsDefault());
		punchTime.setRestFromTime(punchTimeBean.getRestFromTime());
		punchTime.setRestToTime(punchTimeBean.getRestToTime());
		Integer hours=TimeUtils.differMinutes(punchTimeBean.getStartFromTime(), punchTimeBean.getEndFromTime())
				-TimeUtils.differMinutes(restFromTime, restToTime);
		punchTime.setHours(hours);
		punchTime.setUpdateTime(new Date());
		int record = PunchTimeDao.updateByPrimaryKeySelective(punchTime);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除一条考勤设置数据
	 */
	@Override
	public boolean deletePunchTimeList(Integer punchTimeId) {
		PunchTime punchTime=new PunchTime();
		punchTime.setPunchTimeId(punchTimeId);
		punchTime.setIsDelete(1);
		int record = PunchTimeDao.updateByPrimaryKeySelective(punchTime);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据punchTimeId获取一条数据
	 */
	@Override
	public PunchTime getPunchTimeByPunchTimeId(Integer punchTimeId) {
		
		return PunchTimeDao.selectByPrimaryKey(punchTimeId);
	}

	/**
	 * 根据punchTime签到时间判断数据库里是否有相同的数据
	 */
	@Override
	public boolean getPunchTimeByPunchTime(PunchTimeBean punchTimeBean) {
		PunchTimeExample example=new PunchTimeExample();
		PunchTimeExample.Criteria criteria = example.createCriteria();
		criteria.andStartFromTimeEqualTo(punchTimeBean.getStartFromTime());
		criteria.andStartToTimeEqualTo(punchTimeBean.getStartToTime());
		criteria.andEndFromTimeEqualTo(punchTimeBean.getEndFromTime());
		criteria.andEndToTimeEqualTo(punchTimeBean.getEndToTime());
		criteria.andIsDeleteEqualTo(0);
		if(punchTimeBean.getPunchTimeId()!=null) {
			criteria.andPunchTimeIdNotEqualTo(punchTimeBean.getPunchTimeId());
		}
		
		List<PunchTime> punchTimeList=PunchTimeDao.selectByExample(example);
		if(punchTimeList.size()>0){
			return true;
		}else{
			return false;
		}
	}	
}
